package org.jiuling.wechat.officialaccount.config;

import org.jiuling.basics.validation.ObjectVerify;
import org.jiuling.basics.validation.StringVerify;
import org.jiuling.crypto.algorithm.UUID;
import org.jiuling.wechat.officialaccount.enums.MessageEncryptionAndDecryptionMethodEnum;
import org.jiuling.wechat.officialaccount.exception.WeChatOfficialAccountException;

/**
 * 微信公众号配置
 * 获取地址：公众号->设置与开发->开发接口管理->基本配置
 * @author 紫灵
 * @date 2024年11月24日 23时37分37秒
 */
public class WeChatOfficialAccountConfig {

    /** 开发者ID */
    private String appId;
    /** 开发者密码 */
    private String appSecret;
    /** 消息加解密方式 */
    private MessageEncryptionAndDecryptionMethodEnum messageEncryptionAndDecryptionMethod;
    /** 认证Token */
    private String authenticationToken;


    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public MessageEncryptionAndDecryptionMethodEnum getMessageEncryptionAndDecryptionMethod() {
        return messageEncryptionAndDecryptionMethod;
    }

    public void setMessageEncryptionAndDecryptionMethod(MessageEncryptionAndDecryptionMethodEnum messageEncryptionAndDecryptionMethod) {
        this.messageEncryptionAndDecryptionMethod = messageEncryptionAndDecryptionMethod;
    }

    public String getAuthenticationToken() {
        return authenticationToken;
    }

    public void setAuthenticationToken(String authenticationToken) {
        this.authenticationToken = authenticationToken;
    }

    /**
     * 建造者
     * @return
     */
    public static Builder builder(){
    	return new Builder();
    }

    /**
     * 建造者
     */
    public static class Builder{

        /** 微信公众号配置 */
        private final WeChatOfficialAccountConfig weChatOfficialAccountConfig = new WeChatOfficialAccountConfig();

        public Builder addAppId(String appId){
            weChatOfficialAccountConfig.setAppId(appId);
            return this;
        }

        public Builder addAppSecret(String appSecret){
            weChatOfficialAccountConfig.setAppSecret(appSecret);
            return this;
        }

        public Builder addMessageEncryptionAndDecryptionMethod(MessageEncryptionAndDecryptionMethodEnum messageEncryptionAndDecryptionMethod){
            weChatOfficialAccountConfig.setMessageEncryptionAndDecryptionMethod(messageEncryptionAndDecryptionMethod);
            return this;
        }

        public Builder addAuthenticationToken(String authenticationToken){
            weChatOfficialAccountConfig.setAuthenticationToken(authenticationToken);
            return this;
        }

        public WeChatOfficialAccountConfig build() throws WeChatOfficialAccountException {
            if (StringVerify.isBlank(weChatOfficialAccountConfig.appId)){
                throw new WeChatOfficialAccountException("开发者Id不能为空！");
            }
            if (StringVerify.isBlank(weChatOfficialAccountConfig.appSecret)){
                throw new WeChatOfficialAccountException("开发者密码不能为空！");
            }
//            // 认证Token不存在时创建一个认证Token
//            if (StringVerify.isBlank(weChatOfficialAccountConfig.authenticationToken)){
//                weChatOfficialAccountConfig.authenticationToken = UUID.getUUIDIsWithoutLine();
//            }
//            if (ObjectVerify.isNull(weChatOfficialAccountConfig.messageEncryptionAndDecryptionMethod)){
//                throw new WeChatOfficialAccountException("信息加解密方式不能为空！");
//            }
            return weChatOfficialAccountConfig;
        }

    }

}
